﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using DataBasetestApp.ServiceReference2;

namespace DataBasetestApp.Views
{
    public partial class MatchSetupPage : PhoneApplicationPage
    {
        // Items to go in the List picker boxes
        string[] Teams = { "Abbeyleix", "Annanough", "Arles Kilcruise", "Arles Kileen", "Balinakil", "Ballyfin", "Ballylinan", "Ballyroan", "Ballypickas", "Barrowhouse", "Borris In Ossory", "Camross", "Castletown", "Clonad", "Clonaslee St Manamans", "Clough Ballacolla", "Colt", "Courtwood", "Crettyard", "Emo", "Graiguecullen", "Kilcavan", "Kilcotton", "Kileshin", "Kyle", "Mountmellick", "O'Dempseys", "Park Ratheniska", "Portarlington", "Portlaoise", "Raheen", "Rathdowney", "Rosenallis", "Shanahoe", "Slieve Bloom", "St Fintans", "St Joseph's", "Stradbally", "Spink", "The Harps", "The Heath", "The Rock", "Timahoe", "Trumera" };
        string[] Competitions = { "SHC", "IHC", "JHC", "ACHL", "U21HC", "MHC", "MHL", "SFC", "IFC", "JFC", "ACFL", "U21FC", "MFC", "MFL" };
        bool Duplicate = true;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (lpkTeams.SelectedItem != null && lpkTeams1.SelectedItem != null && lpkCompetitions.SelectedItem != null)
            {
                string HomeTeam = lpkTeams.SelectedItem.ToString();
                string AwayTeam = lpkTeams1.SelectedItem.ToString();
                string Competition = lpkCompetitions.SelectedItem.ToString();

                // Method to check for duplicate match already happening
                GAALiveServiceClient client = new GAALiveServiceClient();
                client.CheckForDuplicateCompleted +=
                    new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
                client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
            }
        }

        void CheckForDuplicate()
        {
            string HomeTeam = lpkTeams.SelectedItem.ToString();
            string AwayTeam = lpkTeams1.SelectedItem.ToString();
            string Competition = lpkCompetitions.SelectedItem.ToString();

            GAALiveServiceClient client = new GAALiveServiceClient();
            client.CheckForDuplicateCompleted +=
                new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
            client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
        }

        public MatchSetupPage()
        {
            InitializeComponent();
            this.lpkTeams.ItemsSource = Teams;
            this.lpkTeams1.ItemsSource = Teams;
            this.lpkCompetitions.ItemsSource = Competitions;
            this.lpkTeams2.ItemsSource = Teams;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string HomeTeam = lpkTeams.SelectedItem.ToString();
            string AwayTeam = lpkTeams1.SelectedItem.ToString();
            string Competition = lpkCompetitions.SelectedItem.ToString();
            string Location = lpkTeams2.SelectedItem.ToString();
            string Time;

            if (radioButton2.IsChecked == true)
            {
                Time = radioButton2.Content.ToString();
            }
            else 
            {
                Time = radioButton1.Content.ToString();
            }

            if (HomeTeam == AwayTeam)
            {
                MessageBox.Show("Teams cannot be the same, Please check and try again");
            }

            else
            {
                //MessageBox.Show(string.Format("Are these details correct?" + Environment.NewLine + HomeTeam + " Vs " + AwayTeam + Environment.NewLine + "Competition: " + Competition + Environment.NewLine + "Location: " + Location) MessageBoxButton.OKCancel);
                MessageBoxResult m = MessageBox.Show("Are these details correct?" + Environment.NewLine + HomeTeam + " Vs " + AwayTeam + Environment.NewLine + "Competition: " + Competition + Environment.NewLine + "Location: " + Location, "Confirm", MessageBoxButton.OKCancel);

                if (m == MessageBoxResult.OK)
                {
                    // Checks the result of the duplicate check
                    //Route(HomeTeam, AwayTeam, Location, Competition, Time);
                    if (!Duplicate)
                    {
                        Uri nav = new Uri("/Views/MatchUpdatePage.xaml?HomeTeam=" + HomeTeam + "&AwayTeam=" + AwayTeam + "&Location=" + Location + "&Competition=" + Competition + "&Time=" + Time, UriKind.Relative);
                        NavigationService.Navigate(nav);
                    }
                    else
                    {
                        MessageBox.Show("This match is already being reported on!");
                    }
                    //Duplicate = false;
                }
            }
        }
        void client_CheckForDuplicateCompleted(object sender, CheckForDuplicateCompletedEventArgs c)
        {
            Duplicate = c.Result;
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            if (lpkTeams.SelectedItem != null && lpkTeams1.SelectedItem != null && lpkCompetitions.SelectedItem != null)
            {
                try
                {
                    CheckForDuplicate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                //string HomeTeam = lpkTeams.SelectedItem.ToString();
                //string AwayTeam = lpkTeams1.SelectedItem.ToString();
                //string Competition = lpkCompetitions.SelectedItem.ToString();

                //GAALiveServiceClient client = new GAALiveServiceClient();
                //client.CheckForDuplicateCompleted +=
                //    new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
                //client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
            }
        }

        private void lpkTeams2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpkTeams.SelectedItem != null && lpkTeams1.SelectedItem != null && lpkCompetitions.SelectedItem != null)
            {
                try
                {
                    CheckForDuplicate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                //string HomeTeam = lpkTeams.SelectedItem.ToString();
                //string AwayTeam = lpkTeams1.SelectedItem.ToString();
                //string Competition = lpkCompetitions.SelectedItem.ToString();

                //GAALiveServiceClient client = new GAALiveServiceClient();
                //client.CheckForDuplicateCompleted +=
                //    new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
                //client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
            }
        }

        private void lpkCompetitions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpkTeams.SelectedItem != null && lpkTeams1.SelectedItem != null && lpkCompetitions.SelectedItem != null)
            {
                try
                {
                    CheckForDuplicate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                //string HomeTeam = lpkTeams.SelectedItem.ToString();
                //string AwayTeam = lpkTeams1.SelectedItem.ToString();
                //string Competition = lpkCompetitions.SelectedItem.ToString();

                //GAALiveServiceClient client = new GAALiveServiceClient();
                //client.CheckForDuplicateCompleted +=
                //    new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
                //client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
            }
        }

        private void lpkTeams1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpkTeams.SelectedItem != null && lpkTeams1.SelectedItem != null && lpkCompetitions.SelectedItem != null)
            {
                try
                {
                    CheckForDuplicate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                //string HomeTeam = lpkTeams.SelectedItem.ToString();
                //string AwayTeam = lpkTeams1.SelectedItem.ToString();
                //string Competition = lpkCompetitions.SelectedItem.ToString();

                //GAALiveServiceClient client = new GAALiveServiceClient();
                //client.CheckForDuplicateCompleted +=
                //    new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
                //client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
            }
        }

        private void lpkTeams_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpkTeams.SelectedItem != null && lpkTeams1.SelectedItem != null && lpkCompetitions.SelectedItem != null)
            {
                try
                {
                    CheckForDuplicate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                //string HomeTeam = lpkTeams.SelectedItem.ToString();
                //string AwayTeam = lpkTeams1.SelectedItem.ToString();
                //string Competition = lpkCompetitions.SelectedItem.ToString();

                //GAALiveServiceClient client = new GAALiveServiceClient();
                //client.CheckForDuplicateCompleted +=
                //    new EventHandler<CheckForDuplicateCompletedEventArgs>(client_CheckForDuplicateCompleted);
                //client.CheckForDuplicateAsync(HomeTeam, AwayTeam, Competition);
            }
        }
    }
}